//
//  KASMSLoginViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/11/10.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KASMSLoginViewController.h"
#import <SMS_SDK/SMSSDK.h>

@interface KASMSLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *codeNumTF;
@property(nonatomic, strong) UIButton *rightBtn;
@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, assign) NSInteger time;
@end

@implementation KASMSLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"短信快捷登录";
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [self.timer setFireDate:[NSDate distantFuture]];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    self.time = 59;
    
    [self setTFLeftLalelName:@"手机号:" toTF:_phoneNumTF
     ];
    [self setTFLeftLalelName:@"验证码:" toTF:_codeNumTF
     ];
    _rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
    [_rightBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:KANavTabColor forState:UIControlStateNormal];
    [_rightBtn addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
    _codeNumTF.rightViewMode = UITextFieldViewModeAlways;
    _codeNumTF.rightView = _rightBtn;
}
-(void) countDown
{
    self.time --;
    [self.rightBtn setTitle:[NSString stringWithFormat:@"%lds",self.time] forState:UIControlStateNormal];
    if (self.time == 0) {
        [self.rightBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        self.time = 59;
        [self.timer setFireDate:[NSDate distantFuture]];
    }
}
-(void) sendCode
{
    //判断手机号是否为空
    if ([self.phoneNumTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请填写正确的手机号."];
    } else {
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNumTF.text zone:@"86" result:^(NSError *error) {
            if (!error) {
                [SVProgressHUD showSuccessWithStatus:@"验证码发送成功"];
                [self.timer setFireDate:[NSDate distantPast]];
            } else {
                [SVProgressHUD showErrorWithStatus:@"验证码发送失败"];
                [self.rightBtn setTitle:@"重新发送验证码" forState:UIControlStateNormal];
                self.time = 59;
                [self.timer setFireDate:[NSDate distantFuture]];
            }
        }];
    }
}
-(void) setTFLeftLalelName:(NSString *)name toTF:(UITextField *)tf
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 44)];
    UILabel *leftLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 75, 44)];
    leftLable.center = view.center;
    leftLable.text = name;
    leftLable.font = [UIFont systemFontOfSize:16];
    [view addSubview:leftLable];
    tf.leftViewMode = UITextFieldViewModeAlways;
    tf.rightViewMode = UITextFieldViewModeAlways;
    tf.leftView = view;
}
- (IBAction)smsLoginBtn:(id)sender {
    if ([self.codeNumTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空！"];
    } else {
        [SVProgressHUD showWithStatus:@"正在登录..."];
        [SMSSDK commitVerificationCode:self.codeNumTF.text phoneNumber:self.phoneNumTF.text zone:@"86" result:^(NSError *error) {
            if (!error)  {
                [[NSUserDefaults standardUserDefaults] setObject:self.phoneNumTF.text forKey:@"userName"];
                // 验证成功
                [SVProgressHUD showSuccessWithStatus:@"验证成功"];
                [self.timer invalidate];
                [self.navigationController popViewControllerAnimated:YES];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [SVProgressHUD showErrorWithStatus:@"验证失败"];
            }
        }];
    }
}

@end
